package app.cn.com.school.adapter

import android.content.ContentValues
import android.content.Context
import android.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import app.cn.com.school.R
import app.cn.com.school.databinding.AddressItemBinding

/**
 * Created by Administrator on 2017/11/7 0007.
 */
class AddressAdapter(private val context: Context, private val contentValues: List<ContentValues>, val onClick: setOnClickLoction) : BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView = convertView
        var binding: AddressItemBinding
        if (convertView == null) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.address_item, parent, false)
            convertView = binding.root
            convertView!!.tag = binding
        } else {
            binding = convertView!!.tag as AddressItemBinding
        }
        if (contentValues!!.size <= position || contentValues == null) {
            return convertView
        }

        binding.arsUserEdit.setOnClickListener {
            onClick.edit(position)
        }

        binding.arsUserDelete.setOnClickListener {
            onClick.delete(position)
        }
        return convertView
    }

    override fun getItem(position: Int): Any {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return contentValues?.size ?: 0
    }

    interface setOnClickLoction {
        fun edit(posit: Int)
        fun delete(position: Int)
        fun chose(position: Int, isChecked: Boolean)
    }
}